home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 25 / Cream of the Crop 25.iso / program / gmp202.zip / mpf / tests / t-sub.c < prev   
C/C++ Source or Header  |  1996-06-04  |  3KB  |  123 lines

  1. /* Test mpf_sub.
  2.  
  3. Copyright (C) 1996 Free Software Foundation, Inc.
  4.  
  5. This file is part of the GNU MP Library.
  6.  
  7. The GNU MP Library is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU Library General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.
  11.  
  12. The GNU MP Library is distributed in the hope that it will be useful, but
  13. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14. or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
  15. License for more details.
  16.  
  17. You should have received a copy of the GNU Library General Public License
  18. along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
  19. the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  20. MA 02111-1307, USA. */
  21.  
  22. #include <stdio.h>
  23. #include "gmp.h"
  24. #include "gmp-impl.h"
  25. #include "urandom.h"
  26.  
  27. #ifndef SIZE
  28. #define SIZE 16
  29. #endif
  30.  
  31. #if __STDC__
  32. void ref_mpf_add (mpf_t, const mpf_t, const mpf_t);
  33. void ref_mpf_sub (mpf_t, const mpf_t, const mpf_t);
  34. #else
  35. void ref_mpf_add ();
  36. void ref_mpf_sub ();
  37. #endif
  38.  
  39. main (argc, argv)
  40.      int argc;
  41.      char **argv;
  42. {
  43.   mp_size_t size;
  44.   mp_exp_t exp;
  45.   int reps = 500000;
  46.   int i;
  47.   mpf_t u, v, w, wref;
  48.   mp_size_t bprec = 100;
  49.   mpf_t rerr, max_rerr, limit_rerr;
  50.  
  51.   if (argc > 1)
  52.     {
  53.       reps = strtol (argv[1], 0, 0);
  54.       if (argc > 2)
  55.     bprec = strtol (argv[2], 0, 0);
  56.     }
  57.  
  58.   mpf_set_default_prec (bprec);
  59.  
  60.   mpf_init_set_ui (limit_rerr, 1);
  61.   mpf_div_2exp (limit_rerr, limit_rerr, bprec);
  62. #if VERBOSE
  63.   mpf_dump (limit_rerr);
  64. #endif
  65.   mpf_init (rerr);
  66.   mpf_init_set_ui (max_rerr, 0);
  67.  
  68.   mpf_init (u);
  69.   mpf_init (v);
  70.   mpf_init (w);
  71.   mpf_init (wref);
  72.   for (i = 0; i < reps; i++)
  73.     {
  74.       size = urandom () % (2 * SIZE) - SIZE;
  75.       exp = urandom () % SIZE;
  76.       mpf_random2 (u, size, exp);
  77.  
  78.       size = urandom () % (2 * SIZE) - SIZE;
  79.       exp = urandom () % SIZE;
  80.       mpf_random2 (v, size, exp);
  81.  
  82.       if ((urandom () & 1) != 0)
  83.     mpf_add_ui (u, v, 1);
  84.       else if ((urandom () & 1) != 0)
  85.     mpf_sub_ui (u, v, 1);
  86.  
  87.       mpf_sub (w, u, v);
  88.       ref_mpf_sub (wref, u, v);
  89.  
  90.       mpf_reldiff (rerr, w, wref);
  91.       if (mpf_cmp (rerr, max_rerr) > 0)
  92.     {
  93.       mpf_set (max_rerr, rerr);
  94. #if VERBOSE
  95.       mpf_dump (max_rerr);
  96. #endif
  97.       if (mpf_cmp (rerr, limit_rerr) > 0)
  98.         {
  99.           printf ("ERROR after %d tests\n", i);
  100.           printf ("   u = "); mpf_dump (u);
  101.           printf ("   v = "); mpf_dump (v);
  102.           printf ("wref = "); mpf_dump (wref);
  103.           printf ("   w = "); mpf_dump (w);
  104.           abort ();
  105.         }
  106.     }
  107.     }
  108.  
  109.   exit (0);
  110. }
  111.  
  112. oo (x)
  113.      mpf_t x;
  114. {
  115.   mp_size_t i;
  116.   printf (" exp = %ld\n", x->_mp_exp);
  117.   printf ("size = %d\n", x->_mp_size);
  118.   for (i = ABS (x->_mp_size) - 1; i >= 0; i--)
  119.     printf ("%08lX ", x->_mp_d[i]);
  120.   printf ("\n");
  121.   mpf_dump (x);
  122. }
  123.